package com.rc.saas.tenant.service.tenant;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.rc.saas.tenant.base.BaseService;
import com.rc.saas.tenant.common.sms.AliyunSmsTemplateEnum;
import com.rc.saas.tenant.common.sms.AliyunSmsUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.mapper.tenant.TenantSettingMapper;
import com.rc.saas.tenant.model.tenant.Tenant;
import com.rc.saas.tenant.model.tenant.TenantSetting;
import com.rc.saas.tenant.model.tenant.TenantSettingExample;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.service.elec.ElecPileService;
import com.rc.saas.tenant.service.sms.TenantSmsService;
import org.hibernate.validator.internal.util.stereotypes.Lazy;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author frog
 * @date 2021/6/8
 */
@Service
public class TenantSettingServiceImpl implements TenantSettingService {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TenantSettingMapper tenantSettingMapper;

    @Autowired
    @Lazy
    private ElecPileService elecPileService;

    @Autowired
    private TenantService tenantService;

    @Autowired
    private TenantSmsService tenantSmsService;

    @Override
    public TenantSettingMapper getMapper() {
        return tenantSettingMapper;
    }

    @Override
    public void pileOnlineRateWarn() {

        TenantSettingExample example = new TenantSettingExample();
        List<TenantSetting> tenantSettings = tenantSettingMapper.selectByExample(example);

        if (!CollectionUtils.isEmpty(tenantSettings)) {
            for (TenantSetting tenantSetting : tenantSettings) {
                if ("0".equals(tenantSetting.getOpenPileOnlineWarn())) {
                    if (tenantSetting.getPileOnlineWarnValue() != null && tenantSetting.getPileOnlineWarnMobiles() != null) {
                        //在线桩
                        long loginPileCount = elecPileService.countLoginPile(tenantSetting.getTenantCode());

                        //充电桩总数
                        // 0 : 直流桩  1 :交流桩  2:交直流一体
                        long pileCount = elecPileService.countPile(tenantSetting.getTenantCode(), null);

                        //桩在线率
                        BigDecimal pileOnlineRate = new BigDecimal(loginPileCount).divide(new BigDecimal(pileCount), 2, BigDecimal.ROUND_HALF_UP);

                        Tenant tenant = tenantService.selectByTenantCode(tenantSetting.getTenantCode());

                        if (tenantSetting.getPileOnlineWarnValue() > pileOnlineRate.intValue()) {
                            String[] mobiles = tenantSetting.getPileOnlineWarnMobiles().split(",");
                            for (String mobile : mobiles) {

                                SendSmsResponse sendResult = AliyunSmsUtil.sendSms(mobile, "1234", AliyunSmsTemplateEnum.SEND_TICKET, tenant.getSmsSign());

                                if (sendResult == null) {
                                    logger.error("短信系统异常");
                                }

                                if ("OK".equals(sendResult.getCode())) {
                                    // 更新sms表
                                    tenantSmsService.updateTenantSmsByTenantCode(tenant.getTenantCode(), mobile, "0", "桩群在线率低提醒:" + mobile, "桩群在线率低提醒");
                                }else {
                                    logger.error("短信系统异常:" + sendResult.getMessage());
                                }

                            }
                        }
                    }
                }
            }

        }

    }
}
